﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using LRSTaxServices.Biz;
using LRSTaxServices.Common;

public partial class AdminLRS_ManageTaxNews : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_TaxNews = "__TaxNews::TaxNews";
    private const string PROP_LAYOUT = "__TaxNews::UIL";
    private const string PROP_SELECTED_TaxNews_ID = "__TaxNews::STaxNewsID";

    #endregion

    #region Public Properties

    public TaxNewsCollection DataSourceTaxNews
    {
        get { return (SessionMgr.Session[PROP_DATASRC_TaxNews] is TaxNewsCollection ? (TaxNewsCollection)SessionMgr.Session[PROP_DATASRC_TaxNews] : (TaxNewsCollection)(SessionMgr.Session[PROP_DATASRC_TaxNews] = new TaxNewsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_TaxNews] = value; }
    }

    public int SelectedTaxNewsID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_TaxNews_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_TaxNews_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_TaxNews_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceTaxNews = TaxNews.GetAllTaxNews();
            TaxNewsList1.DataBind(DataSourceTaxNews);
            //divexistname.Visible = false;
        }
    }

    #endregion

    #region Control: TaxNewsList

    protected void ucTaxNewsList_EditRequested(object sender, Controls_TaxNewsList.EditRequestedEventArgs evt)
    {
        SelectedTaxNewsID = evt.TN.TaxNewsID;
        TaxNewsViewEdit1.DataBind(evt.TN);
        SetLayout(LayoutType.Edit);
        //divexistname.Visible = false;
    }


    protected void ucTaxNewsList_AddNewRequested(object sender, EventArgs evt)
    {
        TaxNewsViewEdit1.CancelEdit();
        TaxNews newTaxNews = TaxNews.CreateNew(DataSourceTaxNews);
        SelectedTaxNewsID = newTaxNews.TaxNewsID;
        TaxNewsViewEdit1.DataBind(DataSourceTaxNews.FindByTaxNewsID(SelectedTaxNewsID));
        SetLayout(LayoutType.Edit);
        //divexistname.Visible = false;
    }


    protected void ucTaxNewsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceTaxNews = TaxNews.GetAllTaxNews();
        TaxNewsList1.DataSource = DataSourceTaxNews;
    }

    #endregion

    #region Control: TaxNewsViewEdit

    protected void ucTaxNewsViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        TaxNewsViewEdit1.DataSourceTaxNews = DataSourceTaxNews.FindByTaxNewsID(SelectedTaxNewsID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        TaxNewsViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceTaxNews.FindByTaxNewsID(SelectedTaxNewsID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceTaxNews.Remove(DataSourceTaxNews.FindByTaxNewsID(SelectedTaxNewsID));
                SelectedTaxNewsID = (DataSourceTaxNews[0] != null ? DataSourceTaxNews[0].TaxNewsID : 0);
            }
            catch
            {
            }
        }
        TaxNewsList1.DataBind(DataSourceTaxNews);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (TaxNewsViewEdit1.EndEdit())
        {
            TaxNews obj = DataSourceTaxNews.FindByTaxNewsID(SelectedTaxNewsID);
            TaxNews.Update(obj);
            //Set the feedback text here.....
            DataSourceTaxNews = TaxNews.GetAllTaxNews();
            TaxNewsList1.DataBind(DataSourceTaxNews);
            SetLayout(LayoutType.View);
            Response.AppendHeader("Refresh", "0");
            //divexistname.Visible = false;
        }
        //else
        //{
        //    divexistname.Visible = true;
        //}
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlTaxNewsList.Visible = false;
                pnlTaxNewsViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlTaxNewsList.Visible = true;
                pnlTaxNewsViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
